package com.yaolonga.itfc.manager.linux.netplan;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.setting.yaml.YamlUtil;
import lombok.Data;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: yaolong
 * @CreateTime: 2024-11-13  10:21
 * @Description: netplan 配置实体（针对于详细配置，只有ethernets、 wifis这两个配置项有对应的实体，其他的都用map表示）
 * @Version: 1.0
 */
public class NetplanConfig extends JSONObject {


    public Integer getVersion() {
        return this.getInt("version");
    }

    public void setVersion(Integer version) {
        this.put("version", version);
    }

    public NetplanRenderer getRenderer() {
        return NetplanRenderer.valueOf(this.getStr("renderer"));
    }

    public void setRenderer(NetplanRenderer renderer) {
        this.put("renderer", renderer.name());
    }

    public JSONObject getBonds() {
        return this.getJSONObject("bonds");
    }

    public void setBonds(Map<String, Map<String, Object>> bonds) {
        this.put("bonds", bonds);
    }

    public JSONObject getBridges() {
        return this.getJSONObject("bridges");

    }

    public void setBridges(Map<String, Map<String, Object>> bridges) {
        this.put("bridges", bridges);
    }

    public JSONObject getDummyDevices() {
        return this.getJSONObject("dummy-devices");

    }

    public void setDummyDevices(Map<String, Map<String, Object>> dummyDevices) {
        this.put("dummy-devices", dummyDevices);
    }

    public JSONObject getModems() {
        return this.getJSONObject("modems");

    }

    public void setModems(Map<String, Map<String, Object>> modems) {
        this.put("modems", modems);
    }

    public JSONObject getVirtualEthernets() {
        return this.getJSONObject("virtual-ethernets");

    }

    public void setVirtualEthernets(Map<String, Map<String, Object>> virtualEthernets) {
        this.put("virtual-ethernets", virtualEthernets);
    }

    public JSONObject getVlans() {
        return this.getJSONObject("vlans");

    }

    public void setVlans(Map<String, Map<String, Object>> vlans) {
        this.put("vlans", vlans);
    }

    public JSONObject getVrfs() {
        return this.getJSONObject("vrfs");

    }

    public void setVrfs(Map<String, Map<String, Object>> vrfs) {
        this.put("vrfs", vrfs);
    }

    public JSONObject getNmDevices() {
        return this.getJSONObject("nm-devices");

    }

    public void setNmDevices(Map<String, Map<String, Object>> nmDevices) {
        this.put("nm-devices", nmDevices);
    }

    public JSONObject getWifis() {
        return this.getJSONObject("wifis");

    }

    public void setWifis(Map<String, Map<String, Object>> wifis) {
        this.put("wifis", wifis);
    }

    public JSONObject getEthernets() {
        return this.getJSONObject("ethernets");
    }

    public void setEthernets(Map<String, Map<String, Object>> ethernets) {
        this.put("ethernets", ethernets);
    }


    public JSONArray getRoutes(String interfaceName) {
        JSONObject ethernets = this.getEthernets();
        if (Objects.nonNull(ethernets)) {
            JSONArray byPath = ethernets.getByPath(interfaceName + ".routes", JSONArray.class);
            return Objects.nonNull(byPath) ? byPath : new JSONArray();
        }
        return new JSONArray();
    }


    /**
     * 转换为yml字符串
     *
     * @return yml字符串
     */
    public String toYmlString() {
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(options);
        return yaml.dump(Collections.singletonMap("network", this));
    }


    /**
     * 从yml文件中创建一个配置对象
     *
     * @param path yml文件路径
     * @return 配置对象
     */
    public static NetplanConfig createNetplanConfig(String path) {
        if (path == null) {
            return null;
        }
        JSONObject dict = YamlUtil.loadByPath(path, JSONObject.class);
        return dict.get("network", NetplanConfig.class);
    }

    /**
     * 从yml文件流中创建一个配置对象
     *
     * @param inputStream yml文件流
     * @return 配置对象
     */
    public static NetplanConfig createNetplanConfig(InputStream inputStream) {
        JSONObject dict = YamlUtil.load(inputStream, JSONObject.class);
        return dict.get("network", NetplanConfig.class);
    }

    public static void main(String[] args) {
        NetplanConfig netplanConfig = NetplanConfig.createNetplanConfig("network.yml");
        System.out.println(netplanConfig);
        System.out.println(netplanConfig.toYmlString());
    }

}
